package com.zyn.rsgl.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zyn.rsgl.mapper.LeaveMapper;
import com.zyn.rsgl.pojo.Leave;
import com.zyn.rsgl.service.LeaveService;
import com.zyn.rsgl.utils.commonResult.R;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 请假表 服务实现类
 * </p>
 *
 * @author zcs
 * @since 2025-02-24
 */
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveMapper, Leave> implements LeaveService {

    @Override
    public R listPage(Integer pageNum, Integer pageSize, String startTime, String status, String userId) {
        // 构造查询器
        QueryWrapper<Leave> leaveQueryWrapper = new QueryWrapper<>();
        leaveQueryWrapper.eq("user_id", userId);
        if (startTime != null && !startTime.isEmpty()) {
            // 将 LocalDateTime 转换为 LocalDate
            LocalDate startDate = LocalDate.parse(startTime);
            leaveQueryWrapper.ge("start_time", startDate.atStartOfDay()); // 大于等于当天的开始时间
            leaveQueryWrapper.le("start_time", startDate.atTime(23, 59, 59)); // 小于等于当天的结束时间
        }

        if (status != null && !status.isEmpty()) {
            leaveQueryWrapper.like("status", status);
        }
        // 初始化分页查询
        Page<Object> page = PageHelper.startPage(pageNum, pageSize);
        // 查询
        List<Leave> list = this.list(leaveQueryWrapper);
        if (list != null) {
            return R.ok(list, page.getTotal());
        }
        return R.fail("查询失败");
    }
}
